package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.mapper.ApArticleConfigMapper;
import com.heima.article.service.ApArticleConfigService;
import com.heima.model.article.pojos.ApArticleConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

@Service
@Transactional
@Slf4j
public class ApArticleConfigServiceImpl extends ServiceImpl<ApArticleConfigMapper, ApArticleConfig> implements ApArticleConfigService {


    /**
     * 修改文章配置
     * @param map
     */
    @Override
    public void updateByMap(Map map) {
        //0 下架  1 上架
        Object enable = map.get("enable");
        boolean isDown = true;
        if(enable.equals(1)){
            isDown = false;
        }

        update(Wrappers.<ApArticleConfig>lambdaUpdate()
                .eq(ApArticleConfig::getArticleId,map.get("articleId"))
                .set(ApArticleConfig::getIsDown,isDown));

    }

    /**
     * 修改文章评论状态
     * @param map
     */
    @Override
    public void updateCommentStatusByMap(Map map) {
        Integer operation = Integer.valueOf((String)map.get("operation"));
        boolean isComment = true;
        if (operation.equals(0)){
            isComment = false;
        }
        Long articleId = Long.valueOf((String) map.get("articleId"));
        update(Wrappers.<ApArticleConfig>lambdaUpdate().
                eq(ApArticleConfig::getArticleId,articleId).
                set(ApArticleConfig::getIsComment,isComment));
    }

    /**
     * 根据文章id查文章配置
     * @param articleId
     * @return
     */
    @Override
    public ApArticleConfig findByArticleId(Long articleId) {


        ApArticleConfig apArticleConfig = getOne(Wrappers.<ApArticleConfig>lambdaQuery().eq(ApArticleConfig::getArticleId, articleId));
        if (apArticleConfig == null){
            return null;
        }
        return apArticleConfig;
    }
}
